package autocode;

import com.celesea.framework.util.StringKit;
import com.celesea.workflow.core.dao.WorkflowInstanceDao;
import com.celesea.workflow.core.entity.WorkflowInstance;
import com.google.common.collect.Lists;

import javax.persistence.Column;
import javax.persistence.Table;
import java.lang.reflect.Field;
import java.util.List;

public class MapperUtil {

    private static final String COLUMN = "<result property=\"%s\" column=\"%s\" />";

    public static void main(String[] args) {
        renderMap(WorkflowInstanceDao.class, WorkflowInstance.class);
    }

    public static void renderMap(Class<?> dao, Class<?> clazz) {
        renderMap(dao.getName(), clazz);
    }

    public static void renderMap(String dao, Class<?> clazz) {
        System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
        System.out.println("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >");
        System.out.println();

        Table table = clazz.getAnnotation(Table.class);
        String name = clazz.getName();
        String javaName = getJavaName(clazz.getSimpleName());
        if (StringKit.isEmpty(dao)) {
            dao = name.replace("entity", "dao") + "Dao";
        }

        List<Field> fields = Lists.newArrayList(clazz.getDeclaredFields());
        clazz = clazz.getSuperclass();
        fields.addAll(0, Lists.newArrayList(clazz.getDeclaredFields()));
        clazz = clazz.getSuperclass();
        fields.addAll(0, Lists.newArrayList(clazz.getDeclaredFields()));
        try {
            clazz = clazz.getSuperclass();
            fields.addAll(0, Lists.newArrayList(clazz.getDeclaredFields()));
        } catch (Exception e) {
        }

        StringBuffer buffer = new StringBuffer();
        StringBuffer selectBuffer = new StringBuffer();
        System.out.println("<mapper namespace=\"" + dao + "\">");
        System.out.println("<resultMap type=\"" + name + "\" id=\"" + javaName + "\">");
        boolean first = true;
        for (Field field : fields) {
            Column column = field.getAnnotation(Column.class);
            if (column != null) {
                String colName = column.name().replaceAll("`", "");
                System.out.println(String.format(COLUMN, field.getName(), colName));
                if (first) {
                    first = false;
                    buffer.append(sqlFlag(column.name()));
                    selectBuffer.append(" a.").append(sqlFlag(column.name())).append(" AS ").append(sqlFlag(field.getName()));
                } else {
                    buffer.append(",").append(sqlFlag(column.name()));
                    selectBuffer.append(",").append(" a.").append(sqlFlag(column.name())).append(" AS ").append(sqlFlag(field.getName()));
                }
            }
        }
        System.out.println("</resultMap>");

        System.out.println();
        System.out.println("<sql id=\"columns\">");
        System.out.println(buffer);
        System.out.println("</sql>");
        System.out.println();
        System.out.println("<!-- 查询分页数据列 -->");
        System.out.println("<sql id=\"selectColumns\">");
        System.out.println(selectBuffer);
        System.out.println("</sql>");
        System.out.println();
        System.out.println("<!-- 查询分页数据 -->");
        System.out.println("<select id=\"selectPage\" parameterType=\"com.lz.core.util.QueryParam\" resultType=\"" + name + "\">");
        System.out.println(" select ");
        System.out.println(" <include refid=\"selectColumns\" />");
        System.out.println(" from " + table.name() + " as a ");
        System.out.println("<where>");
        System.out.println("	<if test=\"name != null\">");
        System.out.println("		and `name` like concat('%',#{name},'%')");
        System.out.println("    </if>");
        System.out.println("	<if test=\"code != null\">");
        System.out.println("		and `code` =#{code} ");
        System.out.println("    </if>");
        System.out.println("</where>");
        System.out.println("</select>");
        System.out.println("</mapper>");

    }

    private static String sqlFlag(String column) {
        if (!column.startsWith("`")) {
            column = "`" + column;
        }
        if (!column.endsWith("`")) {
            column = column + "`";
        }
        return column;
    }

    private static String getJavaName(String simpleName) {
        if (simpleName == null) {
            return simpleName;
        }
        return simpleName.substring(0, 1).toLowerCase() + simpleName.substring(1);
    }

}
